package com.ruoyi.generator.util.tpl;

import com.ruoyi.common.constant.GenConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.generator.domain.GenTable;

import java.util.ArrayList;
import java.util.List;

public class VmTplCache implements VmTplInterface{
    /** 项目空间路径 */
    private static final String PROJECT_PATH = "main/java";

    /** mybatis空间路径 */
    private static final String MYBATIS_PATH = "main/resources/mapper";
    /**
     * 获取模板信息
     *
     * @return 模板列表
     */
    public List<String> getTemplateList(String tplCategory)
    {
        String version="cache-vm";
        List<String> templates = new ArrayList<String>();
        templates.add(version+"/java/domain/dao.java.vm");
        templates.add(version+"/java/domain/persist.java.vm");
        templates.add(version+"/java/domain/query.java.vm");
        templates.add(version+"/java/domain/view.java.vm");
        templates.add(version+"/java/domain/querywrapper.java.vm");
        templates.add(version+"/java/domain/viewwrapper.java.vm");
        templates.add(version+"/java/mapper.java.vm");
        templates.add(version+"/java/service.java.vm");
        templates.add(version+"/java/serviceImpl.java.vm");
        templates.add(version+"/java/controller.java.vm");
        templates.add(version+"/xml/mapper.xml.vm");
        templates.add(version+"/js/api.js.vm");
        if (GenConstants.TPL_CRUD.equals(tplCategory))
        {
            templates.add(version+"/vue/index.vue.vm");
        }
        else if (GenConstants.TPL_TREE.equals(tplCategory))
        {
            templates.add(version+"/vue/index-tree.vue.vm");
        }
        else if (GenConstants.TPL_SUB.equals(tplCategory))
        {
            templates.add(version+"/vue/index.vue.vm");
            templates.add(version+"/java/sub-domain.java.vm");
        }
        templates.add(version+"/sql/sql.vm");
        return templates;
    }

    /**
     * 获取文件名
     */
    public String getFileName(String template, GenTable genTable)
    {
        // 文件名称
        String fileName = "";
        // 包路径
        String packageName = genTable.getPackageName();
        // 模块名
        String moduleName = genTable.getModuleName();
        // 大写类名
        String className = genTable.getClassName();
        // 业务名称
        String businessName = genTable.getBusinessName();

        String javaPath = PROJECT_PATH + "/" + StringUtils.replace(packageName, ".", "/");
        String mybatisPath = MYBATIS_PATH + "/" + moduleName;
        String vuePath = "vue";

        if (template.contains("dao.java.vm"))
        {
            fileName = StringUtils.format("{}/domain/dao/{}.java", javaPath, className);
        }
        if (template.contains("persist.java.vm"))
        {
            fileName = StringUtils.format("{}/domain/persist/{}Po.java", javaPath, className);
        }
        if (template.contains("query.java.vm"))
        {
            fileName = StringUtils.format("{}/domain/query/{}Qo.java", javaPath, className);
        }
        if (template.contains("view.java.vm"))
        {
            fileName = StringUtils.format("{}/domain/view/{}Vo.java", javaPath, className);
        }
        if (template.contains("querywrapper.java.vm"))
        {
            fileName = StringUtils.format("{}/domain/wrapper/query/{}WrapperQ.java", javaPath, className);
        }
        if (template.contains("viewwrapper.java.vm"))
        {
            fileName = StringUtils.format("{}/domain/wrapper/view/{}WrapperV.java", javaPath, className);
        }
        if (template.contains("sub-domain.java.vm") && StringUtils.equals(GenConstants.TPL_SUB, genTable.getTplCategory()))
        {
            fileName = StringUtils.format("{}/domain/{}.java", javaPath, genTable.getSubTable().getClassName());
        }
        else if (template.contains("mapper.java.vm"))
        {
            fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, className);
        }
        else if (template.contains("service.java.vm"))
        {
            fileName = StringUtils.format("{}/service/I{}Service.java", javaPath, className);
        }
        else if (template.contains("serviceImpl.java.vm"))
        {
            fileName = StringUtils.format("{}/service/impl/{}ServiceImpl.java", javaPath, className);
        }
        else if (template.contains("controller.java.vm"))
        {
            fileName = StringUtils.format("{}/controller/{}Controller.java", javaPath, className);
        }
        else if (template.contains("mapper.xml.vm"))
        {
            fileName = StringUtils.format("{}/{}Mapper.xml", mybatisPath, className);
        }
        else if (template.contains("sql.vm"))
        {
            fileName = businessName + "Menu.sql";
        }
        else if (template.contains("api.js.vm"))
        {
            fileName = StringUtils.format("{}/api/{}/{}.js", vuePath, moduleName, businessName);
        }
        else if (template.contains("index.vue.vm"))
        {
            fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName);
        }
        else if (template.contains("index-tree.vue.vm"))
        {
            fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName);
        }
        return fileName;
    }
}
